﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.TaskManager;
using Rio.Manager.Business.Repository;
using Rio.Doc.Business.Repository;

namespace Rio.Applications.Web.Services
{
    public partial class BusinessEntitiesDomainService : DomainService
    {
               
        /// <summary>
        /// Get RIO's task manager.
        /// </summary>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public List<TaskItem> GetTaskManager()
        {
            var l_oTaskManager = new List<TaskItem>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("TaskManagerDS.GetTaskManager");
                l_oTaskManager = TaskManagerRepository.GetTaskManager();
            }
            catch (Exception ex)
            {
                l_oTaskManager.Add(new TaskItem("Exception in TaskManagerDS.GetTaskManager : " + ex.Message));
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in TaskManagerDS.GetTaskManager: " + ex.Message);
            }
            return l_oTaskManager;
        }

        /// <summary>
        /// Add new task manager.
        /// </summary>
        /// <param name="p_oTaskToAdd">Task to add.</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public void AddTaskManager(TaskItem p_oTaskToAdd)
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("TaskManagerDS.AddTaskManager: " + p_oTaskToAdd.ToString());
                TaskManagerRepository.AddTaskManager(p_oTaskToAdd);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in TaskManagerDS.AddTaskManager: " + ex.Message); 
            }
        }

        /// <summary>
        /// Delete RIO's task manager list.
        /// </summary>
        /// <param name="p_oTaskToDelete">Task to delete</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public void DeleteTaskManager(TaskItem p_oTaskToDelete)
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("TaskManagerDS.DeleteTaskManager:" + p_oTaskToDelete.ToString());
                TaskManagerRepository.DeleteTaskManager(p_oTaskToDelete);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in TaskManagerDS.DeleteTaskManager: " + ex.Message);
            }
        }

        /// <summary>
        /// Delete all RIO's task manager.
        /// </summary>
        /// <param name="p_oTaskToDelete">Delete all the task</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public void DeleteAllTaskManager()
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("TaskManagerDS.DeleteAllTaskManager:" );
                TaskManagerRepository.DeleteAllTaskManager();
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in TaskManagerDS.DeleteAllTaskManager: " + ex.Message);
            }
        }     

        /// <summary>
        /// Update RIO's task manager list.
        /// </summary>
        /// <param name="p_oTaskToUpdate">Task to update</param>
        /// <param name="p_sOldAction">Old action value if it's modify</param>
        /// <param name="p_sOldStartHour">Old start hour if it's modify</param>
        /// <returns></returns>
        [Invoke(HasSideEffects = true)]
        public void UpdateTaskManager(TaskItem p_oTaskToUpdate, string p_sOldAction = "", string p_sOldStartHour = "00:00")
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("TaskManagerDS.UpdateTaskManager:" + p_oTaskToUpdate.ToString());
                TaskManagerRepository.UpdateTaskManager(p_oTaskToUpdate, p_sOldAction,p_sOldStartHour);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in TaskManagerDS.UpdateTaskManager: " + ex.Message);
            }
        }

    }
}